#!/usr/bin/env python
# -*- coding: utf-8 -*-

print("测试脚本开始执行")

try:
    import pymysql
    print("成功导入pymysql模块")
except ImportError:
    print("无法导入pymysql模块，请确保已安装: pip install pymysql")
    import sys
    sys.exit(1)

# 数据库配置
db_config = {
    'host': 'localhost',
    'user': 'root',
    'passwd': '123456',
    'db': 'premade_food_trace',
    'charset': 'utf8mb4',
}

print(f"尝试连接数据库: {db_config}")

try:
    # 连接数据库
    conn = pymysql.connect(
        host=db_config['host'],
        user=db_config['user'],
        password=db_config['passwd'],  # pymysql使用password而不是passwd
        database=db_config['db'],      # pymysql使用database而不是db
        charset=db_config['charset']
    )
    
    print("数据库连接成功!")
    
    # 检查suppliers表
    cursor = conn.cursor()
    cursor.execute("SELECT COUNT(*) FROM suppliers")
    count = cursor.fetchone()[0]
    print(f"suppliers表中有 {count} 条记录")
    
    cursor.close()
    conn.close()
    print("数据库连接已关闭")
    
except Exception as e:
    print(f"连接数据库时出错: {str(e)}")
    import traceback
    traceback.print_exc()

print("测试脚本执行完毕") 